﻿using System;
using System.Collections.Generic;

using E.BLL;
using E.Model;
using E.Web.UI.Page;

using X.Common;

namespace E.Web.Teacher.exam
{
    public partial class Export2Xls2 : TeacherBasePage
    {
        public string act;
        public string userName, fromDate, toDate;
        public string classCode;
        public int areaid;
        public int certificateID;
        public int courseID;
        public string startFromDate = "";
        public string startToDate = "";
        public int courseCategoryID;
        public string loginid;

        public IList<ScoreCategoryModel> scoreCategoryList;

        protected override void ShowPage()
        {
            act = XRequest.GetString("act");
            loginid = XRequest.GetString("LoginIDTxt");
            userName = XRequest.GetString("UserNameTxt");
            classCode = XRequest.GetString("ClassCodeTxt");
            courseID = XRequest.GetInt("CourseSlt", -1);
            certificateID = XRequest.GetInt("CertificateSlt", -1);
            courseCategoryID = XRequest.GetInt("CategorySlt", -1);

            IList<DictAreaModel> areaList = GetTeacherAreaList(teacherid);

            if (areaList.Count == 1)
            {
                areaid = areaList[0].ID;
            }
            else
            {
                areaid = XRequest.GetInt("AreaSlt", -1);
            }


            InitSearchDateTime();

            if (ispost || act == "s")
            {
                ExamBLL bll = new ExamBLL();

                scoreCategoryList = bll.GetScoreCategoryList();

                SearchScoreModel model = new SearchScoreModel();
                model.LoginID = loginid;
                model.UserName = userName;
                model.AreaID = areaid;
                model.CertificateID = certificateID;
                model.CourseID = courseID;
                model.ClassCode = classCode;
                model.StartFromDate = startFromDate;
                model.StartToDate = startToDate;
                model.CourseCategoryID = courseCategoryID;
                model.EntryFromDate = fromDate;
                model.EntryToDate = toDate;

                if (authority != E.Enum.EnumTeacherAuthority.Admin)
                {
                    model.TeacherID = teacherid;
                }
                else
                {
                    model.TeacherID = -1;
                }

                //model.TeacherID = teacherid;

                IList<ShortScoreModel> list = bll.GetAllScore(model);

                if (list.Count > 0)
                {
                    try
                    {
                        System.Text.StringBuilder sb = ExportTable(list);
                        //System.Web.HttpContext.Current.Response.Write(sb.ToString());
                        ExportData(sb);
                    }
                    catch (Exception e)
                    {
                        System.Web.HttpContext.Current.Response.Write("数据读取错误");
                    }
                }
                else
                {
                    System.Web.HttpContext.Current.Response.Write("暂无数据");
                }
                //ExportData(sb);
            }
        }

        private void ExportData(System.Text.StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.Buffer = true;
            //System.Web.HttpContext.Current.Response.Charset = "GB2312";
            System.Web.HttpContext.Current.Response.Charset = "UTF-8";
            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.DateTime.Now.ToString("_yyMMdd_hhmm") + ".xls");
            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
            System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
            //System.Web.HttpContext.Current.e = false;
            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }

        private void InitSearchDateTime()
        {
            try
            {
                if (!XRequest.GetString("StartFromDateTxt").Equals(""))
                {
                    Convert.ToDateTime(XRequest.GetString("StartFromDateTxt"));
                    startFromDate = XRequest.GetString("StartFromDateTxt");
                }

                if (!XRequest.GetString("StartToDateTxt").Equals(""))
                {
                    Convert.ToDateTime(XRequest.GetString("StartToDateTxt"));
                    startToDate = XRequest.GetString("StartToDateTxt");
                }
                if (!XRequest.GetString("FromDateTxt").Equals(""))
                {
                    Convert.ToDateTime(XRequest.GetString("FromDateTxt"));
                    fromDate = XRequest.GetString("FromDateTxt");
                }

                if (!XRequest.GetString("ToDateTxt").Equals(""))
                {
                    Convert.ToDateTime(XRequest.GetString("ToDateTxt"));
                    toDate = XRequest.GetString("ToDateTxt");
                }
            }
            catch (Exception ex)
            {

            }
        }

        private System.Text.StringBuilder ExportTable(IList<ShortScoreModel> list)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder("");

            if (scoreCategoryList != null)
            {
                //sb.Append("<tr><td>登录名</td><td>姓名</td><td>性别</td><td>学历</td><td>认证或课程</td><td>成绩</td></tr>");

                ExamBLL bll = new ExamBLL();

                foreach (ShortScoreModel model in list)
                {
                    sb.Append("<table border=\"1\" cellpadding=\"6\" cellspacing=\"2\" style=\"width:600px; font:12px '宋体'; border-collapse:collapse; border-color:#ccc; border-width:1px;\">\r\n");
                    sb.Append("  <tr>\r\n");
                    sb.Append("    <td colspan=\"4\" style=\"font-weight:bolder;\">基本信息</td>\r\n");
                    sb.Append("  </tr>\r\n");
                    sb.Append("  <tr>\r\n");
                    sb.Append("    <td>登录名：</td><td>" + model.LoginID + "</td><td>姓名：</td><td>" + model.UserName + "</td>\r\n");
                    sb.Append("  </tr>\r\n");
                    sb.Append("  <tr>\r\n");
                    sb.Append("    <td>性别：</td><td>" + (model.Gender ? "男" : "女") + "</td><td>学历：</td><td>" + model.Education + "</td>\r\n");
                    sb.Append("  </tr>\r\n");
                    sb.Append("  <tr>\r\n");
                    sb.Append("    <td colspan=\"4\" style=\"font-weight:bolder;\">课程成绩统计</td>\r\n");
                    sb.Append("  </tr>\r\n");

                    IList<ShortScoreModel> scoreList = bll.GetUserCourseScoreList(model.UserID);

                    if (scoreList.Count > 0)
                    {
                        sb.Append("  <tr style=\"font-weight:bolder;\">\r\n");
                        sb.Append("    <td>课程名称</td><td>分数</td><td colspan=\"2\">考试时间</td>\r\n");
                        sb.Append("  </tr>\r\n");
                        foreach (ShortScoreModel scoreModel in scoreList)
                        {
                            sb.Append("  <tr>\r\n");
                            sb.Append("    <td>" + scoreModel.CourseCertificateName + "</td><td>" + scoreModel.Score + "</td><td colspan=\"2\">" + scoreModel.StartTime.ToString("yyyy.MM.dd") + "</td>\r\n");
                            sb.Append("  </tr>\r\n");
                        }
                    }
                    else
                    {
                        sb.Append("  <tr>\r\n");
                        sb.Append("    <td colspan=\"4\">无数据</td>\r\n");
                        sb.Append("  </tr>\r\n");
                    }

                    sb.Append("  <tr>\r\n");
                    sb.Append("    <td colspan=\"4\" style=\"font-weight:bolder;\">认证成绩统计</td>\r\n");
                    sb.Append("  </tr>\r\n");
                    sb.Append("  <tr style=\"font-weight:bolder;\">\r\n");
                    sb.Append("    <td>成绩种类</td><td>分数</td><td colspan=\"2\">考试时间</td>\r\n");
                    sb.Append("  </tr>\r\n");

                    if (scoreCategoryList.Count > 0)
                    {
                        decimal total = 0;

                        foreach (ScoreCategoryModel categoryModel in scoreCategoryList)
                        {
                            decimal c = 0;

                            sb.Append("  <tr>\r\n");
                            sb.Append("    <td>" + categoryModel.Name + "</td>");

                            if (categoryModel.IsInput)
                            {
                                int score = bll.GetSetScore(model.UserID, categoryModel.ID);
                                c = Math.Round((decimal)score * categoryModel.Scale / 100, 2);
                                sb.Append("<td>" + c + "</td><td colspan=\"2\">&nbsp;</td>\r\n");
                            }
                            else
                            {
                                if (categoryModel.IsCourse)
                                {
                                    IList<int> sList = bll.GetUserCourseScore(model.UserID);
                                    int totalScore = 0;
                                    foreach (int i in sList)
                                    {
                                        totalScore += i;
                                    }
                                    c = Math.Round((decimal)(totalScore) * categoryModel.Scale / 50, 2);
                                    sb.Append("<td>" + c + "</td><td colspan=\"2\">&nbsp;</td>\r\n");
                                }
                                else
                                {
                                    ShortScoreModel ssmodel = bll.GetCertificateScore(model.UserID, areaid);
                                    if (ssmodel.ID != -1)
                                    {
                                        c = Math.Round((decimal)(ssmodel.Score) * categoryModel.Scale / 100, 2);
                                        sb.Append("<td>" + c + "</td><td colspan=\"2\">" + ssmodel.StartTime.ToString("yyyy.MM.dd") + "</td>\r\n");
                                    }
                                    else
                                    {
                                        sb.Append("<td>0</td><td colspan=\"2\">&nbsp;</td>\r\n");
                                    }
                                }
                            }
                            sb.Append("  </tr>\r\n");

                            total += c;
                        }

                        sb.Append("  <tr>\r\n");
                        sb.Append("    <td colspan=\"4\">总分：" + total + "</td>\r\n");
                        sb.Append("  </tr>\r\n");
                    }
                    else
                    {
                        sb.Append("  <tr>\r\n");
                        sb.Append("    <td colspan=\"4\">无数据</td>\r\n");
                        sb.Append("  </tr>\r\n");
                    }

                    sb.Append("</table>\r\n<p>&nbsp;</p>\r\n");
                }
            }
            return sb;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
